package com.cgw.game.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.cgw.game.utils.DensityUtil;

import java.util.Date;

public class GameView extends View {
    /**
     * 圆点画笔
     * */
    private Paint mPaint;
    /**
     * 文字画笔
     * */
    private Paint mTextPaint;
    /**
     * 宽
     * */
    private int width;
    /**
     * 高
     * */
    private int height;
    private Context context;

    /**
     * 时间差值
     * */
    private long differenceTime = 0;
    /**
     * 计时器
     * */
    private CountDownTimer timer;
    /**
     *监听器
     * */
    private OnGameLinsener linsener;
    private String countTimeStr = "05:00";

    /**
     * 是否游戏中
     * */
    private boolean isPlay = false;
    /**
     * 是否红点显示状态
     * */
    private boolean isAppear= false;
    /**
     * 红点显示开始时间
     * */
    private long appearStarTime = 0;
    /**
     * 1秒判断
     * */
    private boolean isOnesecond =false;
    public GameView(Context context) {
        this(context,null);
    }

    public GameView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public GameView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
    /**
     * 初始化
     * */
    private void initView() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(DensityUtil.dip2px(context,3));
        mTextPaint = new Paint();
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setAntiAlias(true);

    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = measureSize(widthMeasureSpec);
        height = measureSize(heightMeasureSpec);
        setMeasuredDimension(width,height);
    }
    private int measureSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = 500;
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLines(new float[]{
                0f,0f,width,0,
                width,0,width,height,
                0,height,width,height,
                0,0,0,height
        },mPaint);
        if(isPlay&&isAppear){
            canvas.drawCircle(width/2,height/2, DensityUtil.dip2px(context,20f),mPaint);
        }
        mTextPaint.setTextSize(DensityUtil.dip2px(context,15f));
        int textWidth = (int)mTextPaint.measureText(countTimeStr);
        canvas.drawText(countTimeStr,width/2-textWidth/2,50,mTextPaint);
        if(differenceTime!=0){
            mTextPaint.setTextSize(DensityUtil.dip2px(context,10f));
            canvas.drawText(differenceTime+"ms",width-130,height-100,mTextPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                float x = event.getX();
                float y = event.getX();
                if(isPlay&&isAppear){
                    if(linsener!=null){
                        differenceTime = new Date().getTime()-appearStarTime;
                        linsener.onTonch(isPointInCircle(new PointF(x,y),new PointF(width/2,height/2),DensityUtil.dip2px(context,20f)),(int)differenceTime);
                        isAppear = false;
                        isOnesecond =false;
                        invalidate();
                    }
                }
                break;
            case MotionEvent.ACTION_UP:

                break;
            case MotionEvent.ACTION_CANCEL:
                differenceTime = 0;
                break;
        }
        return true;
    }
    /**
     * 判断点是否在圆内
     *
     * @param pointF 待确定点
     * @param circle 圆心
     * @param radius 半径
     * @return true在圆内
     */
    private boolean isPointInCircle(PointF pointF, PointF circle, float radius) {
        return Math.pow((pointF.x - circle.x), 2) + Math.pow((pointF.y - circle.y), 2) <= Math.pow(radius, 2);
    }
    public void startGame(int countTime){
        isPlay = true;
        isAppear  = true;
        isOnesecond = true;
        appearStarTime = new Date().getTime();
        timer = new CountDownTimer(countTime,1000){
            @Override
            public void onTick(long millisUntilFinished) {
                int secondTime = (int)(millisUntilFinished/1000%60);
                int minTime = (int)(millisUntilFinished/1000/60);
                String secondTimeStr = secondTime+"";
                if(secondTime<10){
                    secondTimeStr = "0"+secondTime;
                }
                String minTimeStr = secondTime+"";
                if(minTime<10){
                    minTimeStr = "0"+minTime;
                }
                if((int)(millisUntilFinished/1000)%5==0){
                    isAppear  = true;
                    isOnesecond = true;
                    appearStarTime = new Date().getTime();
                }else {
                    if(isOnesecond&&isAppear){
                        isAppear = false;
                        isOnesecond = false;
                        if(linsener!=null){
                            linsener.onTonch(false,-1);
                        }
                    }
                }

                countTimeStr = minTimeStr+":"+secondTimeStr;
                invalidate();
            }

            @Override
            public void onFinish() {
                if(linsener!=null){
                    linsener.onEnd();
                }
                isPlay = false;
                invalidate();
            }
        }.start();
    }
    public interface OnGameLinsener{
        void onEnd();
        void onTonch(boolean isTonch,int differenceTime);
    }

    public void setLinsener(OnGameLinsener linsener) {
        this.linsener = linsener;
    }
}
